<?php
namespace app\admin\controller;
use app\common\controller\AdminBase;
/*单车预约*/
class BikeOrder extends AdminBase{
	protected $beforeActionList = [
        'before_index'=>['only'=>'index'],
    ];
	function _initialize() {
		parent::_initialize();	
    }
	/*单车预约列表*/
	function index(){
		$this->assign("list",$this->_get_all_set());
		$this->assign("device",$this->_get_class_device());
		echo $this->fetch();
	}
	/*编辑时间*/	
	function set_post(){
		$step=input("step");
		$shop_id=sp_get_current_shop_id();
		cache("BikeOrder_".$shop_id,null);
		if(empty($step)){
			$this->ajax_error("参数错误");
		}else{
			$count=db("Class")->where("shop_id=$shop_id and step=$step")->count(1);
			if($count<1){
				db("Class")->insert(
					array("shop_id"=>$shop_id,
						  "order_start_time"=>input("order_start_time"),
						  "order_end_time"=>input("order_end_time"),
						  "step"=>input("step"),
						  "class_start_time"=>input("class_start_time"),
						  "class_end_time"=>input("class_end_time"),
						  "class_name"=>$this->_get_class_name($step),
						  "pcount"=>input("pcount")));
			}else{
				db("Class")->where("shop_id=$shop_id and step=$step")->update(
					array(
						  "order_start_time"=>input("order_start_time"),
						  "order_end_time"=>input("order_end_time"),
						  "class_start_time"=>input("class_start_time"),
						  "class_end_time"=>input("class_end_time"),
						  "pcount"=>input("pcount")));
			}
		}
		$this->ajax_success();
	}
	/*清空设置*/
	function clear_set(){
		$step=input("step");
		$shop_id=sp_get_current_shop_id();
		cache("BikeOrder_".$shop_id,null);
		if(empty($step)){
			$this->ajax_error("参数错误");
		}else{
			db("Class")->where("shop_id=$shop_id and step=$step")->delete();
		}
		$this->ajax_success();
	}
   
    /*预约设备设置*/
    function  device_set(){
		$device_no=input("device_no");
		$shop_id=sp_get_current_shop_id();
		cache("BikeOrderDevice_".$shop_id,null);
		if(empty($device_no)){
			$this->ajax_error("参数错误");
		}else{
			$count=db("ClassDevice")->where("shop_id=$shop_id and device_no='$device_no'")->count(1);
			if($count<1){
				db("ClassDevice")->insert(
					array("shop_id"=>$shop_id,
						  "device_name"=>input("device_name"),
						  "device_code"=>input("device_code"),
						  "device_no"=>input("device_no"),
						  	)
						);
			}else{
				db("ClassDevice")->where("shop_id=$shop_id and device_no='$device_no'")->update(
					array(
						 "device_name"=>input("device_name"),
						  "device_code"=>input("device_code"),
						  "device_no"=>input("device_no"),
						  )
					);
			}
		}
		$this->ajax_success();
    }
	
	/*预约设备清空*/
	function clear_device(){
		$device_code=input("device_code");
		$shop_id=sp_get_current_shop_id();
		cache("BikeOrderDevice_".$shop_id,null);
		if(empty($device_code)){
			$this->ajax_error("参数错误");
		}else{
			db("ClassDevice")->where("shop_id=$shop_id and device_code='$device_code'")->delete();
		}
		$this->ajax_success();
	}
	
	/*
	 * 获取所有店铺设置
	 * */	
	function  _get_all_set(){
		$shop_id=sp_get_current_shop_id();
		$init_list=$this->_get_init_set();
		$list=db("Class")->where("shop_id=$shop_id")->column("step,order_start_time,order_end_time,class_start_time,class_end_time,pcount,class_name");
		if(empty($list)){
			return $init_list;
		}else{
			foreach ($list as $key => $value) {
				$init_list[$key]=$value;
			}
		}
		return $init_list;
	}

	/*获取初始化课程*/
    function _get_init_set(){
    	return array(
			"1"=>array("order_start_time"=>"00:00","order_end_time"=>"00:00","step"=>1,"class_start_time"=>"00:00","class_end_time"=>"00:00","class_name"=>"第一场","pcount"=>1),
			"2"=>array("order_start_time"=>"00:00","order_end_time"=>"00:00","step"=>2,"class_start_time"=>"00:00","class_end_time"=>"00:00","class_name"=>"第二场","pcount"=>1),
			"3"=>array("order_start_time"=>"00:00","order_end_time"=>"00:00","step"=>3,"class_start_time"=>"00:00","class_end_time"=>"00:00","class_name"=>"第三场","pcount"=>1),
			"4"=>array("order_start_time"=>"00:00","order_end_time"=>"00:00","step"=>4,"class_start_time"=>"00:00","class_end_time"=>"00:00","class_name"=>"第四场","pcount"=>1),
			"5"=>array("order_start_time"=>"00:00","order_end_time"=>"00:00","step"=>5,"class_start_time"=>"00:00","class_end_time"=>"00:00","class_name"=>"第五场","pcount"=>1),
		);
    }
    /*获取课程名称*/
    function _get_class_name($step){
    	$arr=array("1"=>"第一场","2"=>"第二场","3"=>"第三场","4"=>"第四场","5"=>"第五场");
		return $arr[$step];
    }
	///////////////////////////////////////////////////////////////////////////////////
	/**
	 * 权限菜单
	 **/
	function before_index(){
	}
	
	/*获取预约设备*/
	private function _get_class_device(){
		$shop_id=sp_get_current_shop_id();
		$list=db("ClassDevice")->where("shop_id=$shop_id")->select();
		if(empty($list)){
			return array("device_name"=>"","device_no"=>"1","device_code"=>"0");
		}else{
			return $list[0];
		}
	}
	///////////////////////////////////////////////////////////////////////////////////
}